#coding=utf-8
#Boa:Dialog:DlgOperatorEditor

import wx
from product import CDataStaff
from ctrlAider import MyComboBoxDecorator

def create(parent):
    return DlgOperatorEditor(parent)

[wxID_DLGOPERATOREDITOR, wxID_DLGOPERATOREDITORBTNCANCEL, 
 wxID_DLGOPERATOREDITORBTNSAVE, wxID_DLGOPERATOREDITORCBLIMITED, 
 wxID_DLGOPERATOREDITORTCPSW, wxID_DLGOPERATOREDITORTCPSWCONFIRM, 
 wxID_DLGOPERATOREDITORTCSTAFF, wxID_DLGOPERATOREDITORTCUSER, 
 wxID_DLGOPERATOREDITORTPSW, wxID_DLGOPERATOREDITORTPSWCONFIRM, 
 wxID_DLGOPERATOREDITORTSTAFF, wxID_DLGOPERATOREDITORTUSER, 
] = [wx.NewId() for _init_ctrls in range(12)]

class DlgOperatorEditor(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGOPERATOREDITOR,
              name=u'DlgOperatorEditor', parent=prnt, pos=wx.Point(349, 211),
              size=wx.Size(346, 367), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u64cd\u4f5c\u5458\u7f16\u8f91')
        self.SetClientSize(wx.Size(338, 333))

        self.tStaff = wx.StaticText(id=wxID_DLGOPERATOREDITORTSTAFF,
              label=u'\u5458\u5de5\uff1a', name=u'tStaff', parent=self,
              pos=wx.Point(40, 40), size=wx.Size(36, 14), style=0)

        self.tUser = wx.StaticText(id=wxID_DLGOPERATOREDITORTUSER,
              label=u'\u7528\u6237\u540d\uff1a', name=u'tUser', parent=self,
              pos=wx.Point(40, 88), size=wx.Size(48, 14), style=0)

        self.tcUser = wx.TextCtrl(id=wxID_DLGOPERATOREDITORTCUSER,
              name=u'tcUser', parent=self, pos=wx.Point(104, 80),
              size=wx.Size(152, 22), style=0, value=u'')

        self.tPsw = wx.StaticText(id=wxID_DLGOPERATOREDITORTPSW,
              label=u'\u767b\u5f55\u5bc6\u7801\uff1a', name=u'tPsw',
              parent=self, pos=wx.Point(40, 136), size=wx.Size(60, 14),
              style=0)

        self.tcPsw = wx.TextCtrl(id=wxID_DLGOPERATOREDITORTCPSW, name=u'tcPsw',
              parent=self, pos=wx.Point(104, 128), size=wx.Size(160, 22),
              style=wx.TE_PASSWORD, value=u'')

        self.tPswConfirm = wx.StaticText(id=wxID_DLGOPERATOREDITORTPSWCONFIRM,
              label=u'\u5bc6\u7801\u786e\u8ba4\uff1a', name=u'tPswConfirm',
              parent=self, pos=wx.Point(40, 184), size=wx.Size(60, 14),
              style=0)

        self.tcPswConfirm = wx.TextCtrl(id=wxID_DLGOPERATOREDITORTCPSWCONFIRM,
              name=u'tcPswConfirm', parent=self, pos=wx.Point(104, 176),
              size=wx.Size(160, 22), style=wx.TE_PASSWORD, value=u'')

        self.btnSave = wx.Button(id=wxID_DLGOPERATOREDITORBTNSAVE,
              label=u'\u4fdd\u5b58', name=u'btnSave', parent=self,
              pos=wx.Point(48, 256), size=wx.Size(75, 24), style=0)
        self.btnSave.Bind(wx.EVT_BUTTON, self.OnBtnSaveButton,
              id=wxID_DLGOPERATOREDITORBTNSAVE)

        self.btnCancel = wx.Button(id=wxID_DLGOPERATOREDITORBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(192, 256), size=wx.Size(75, 24), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGOPERATOREDITORBTNCANCEL)

        self.tcStaff = wx.TextCtrl(id=wxID_DLGOPERATOREDITORTCSTAFF,
              name=u'tcStaff', parent=self, pos=wx.Point(104, 32),
              size=wx.Size(152, 22), style=0, value=u'')

        self.cbLimited = wx.CheckBox(id=wxID_DLGOPERATOREDITORCBLIMITED,
              label=u'\u53d7\u9650\u8d26\u53f7', name=u'cbLimited', parent=self,
              pos=wx.Point(40, 224), size=wx.Size(79, 14), style=0)
        self.cbLimited.SetValue(True)

    def __init__(self, parent):
        self._init_ctrls(parent)

        
##    def myInit(self):
##        self.staffList = MyComboBoxDecorator(self.cbStaff, CDataStaff, 
##                                    CDataStaff.COL_NAME)
##        
##        self.staffList.refresh()
    
    def setStaff(self, staff):
        self.staffObj = staff

        self.tcStaff.SetValue(staff.getValue(CDataStaff.COL_NAME))
        self.tcUser.SetValue(staff.getValue(CDataStaff.COL_USER))
        self.tcPsw.SetValue(staff.getValue(CDataStaff.COL_PSW))
        self.tcPswConfirm.SetValue(staff.getValue(CDataStaff.COL_PSW))
        self.cbLimited.SetValue(staff.getValue(CDataStaff.COL_LIMITED))
            
    def OnBtnSaveButton(self, event):
##        if (self.staffList.GetValue() == ''):
##            dlg = wx.MessageDialog(self, u'没有设置员工，必须设置员工', u'提示',
##                                    wx.OK|wx.ICON_ERROR)
##            dlg.ShowModal()
##            return

        if (self.tcUser.GetValue() == ''):
            dlg = wx.MessageDialog(self, u'没有设置用户名，必须设置用户名', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        if (self.tcPsw.GetValue() != self.tcPswConfirm.GetValue()):
            dlg = wx.MessageDialog(self, u'密码不一致，请重新输入', u'提示',
                                    wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        self.saveOperator()
        self.Close()
        
    def saveOperator(self):
        data = {CDataStaff.COL_ISOPERATOR:'T',
                CDataStaff.COL_USER:self.tcUser.GetValue(),
                CDataStaff.COL_PSW:self.tcPsw.GetValue(),
                CDataStaff.COL_LIMITED:1 if self.cbLimited.GetValue() else 0}
        
        self.staffObj.setMulFieldVal(data)
        self.staffObj.update()
        
    def OnBtnCancelButton(self, event):
        self.Close()
    
        
